package edu.ruc.cluster.test;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import edu.ruc.cluster.util.JDKFileFilter;

public class YFileChooser implements ActionListener{
	JFrame frame=new JFrame("FileChooserExample");
	JTabbedPane tabPane=new JTabbedPane();//tab layout
	Container con=new Container();//layout 1
	Container con1=new Container();//layout 2
	JLabel label1=new JLabel("choose directory");
	JLabel label2=new JLabel("choose file");
	JTextField text1=new JTextField();
	JTextField text2=new JTextField();
	JButton button1=new JButton("...");
	JButton button2=new JButton("...");
	JFileChooser jfc=new JFileChooser();//file filter
	YFileChooser(){
		jfc.setCurrentDirectory(new File("~/"));//initial directory
		//get screen height and weight
		double lx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setLocation(new Point((int)(lx/2)-150,(int)(ly/2)-150));//setup window appear location
		frame.setSize(500,150);//setup window size
		frame.setContentPane(tabPane);//set layout
       //setup tab location and height, weight
		label1.setBounds(10,10,170,20);
		label2.setBounds(10,30,200,20);
		text1.setBounds(150,10,120,20);
		text2.setBounds(150,30,120,20);
		button1.setBounds(270,10,50,20);
		button2.setBounds(270,30,50,20);
		
		button1.addActionListener(this);//action
		button2.addActionListener(this);//action
		con.add(label1);
		con.add(label2);
		con.add(text1);
		con.add(text2);
		con.add(button1);
		con.add(button2);
		con.add(jfc);
		tabPane.add("directory/file",con);//tab1
		tabPane.add("no-content",con1);//tab2
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//ok to close
	}                             
	public void actionPerformed(ActionEvent e){
		if(e.getSource().equals(button1)){//get source
			jfc.setFileSelectionMode(1);//can only choose directory
			int state=jfc.showOpenDialog(null);//open select dialog
			if(state==1){
				return;//cancel
			}
			else{
				File f=jfc.getSelectedFile();//f is selected directory
				text1.setText(f.getAbsolutePath());
			}
		}
		if(e.getSource().equals(button2)){
//			jfc.setFileSelectionMode(0);//can only select file
			jfc.setFileFilter(new JDKFileFilter());
			int state=jfc.showOpenDialog(null);//open select dialog
			if(state==1){
				return;
			}
			else{
				File f=jfc.getSelectedFile();//f is the selected file
				text2.setText(f.getAbsolutePath());
			}
		}
	}
	public static void main(String[] args) {
		new YFileChooser();

	}

}
